package com.lu.library.http

import com.blankj.utilcode.util.ToastUtils
import com.google.gson.JsonParseException
import kotlinx.coroutines.CoroutineExceptionHandler
import java.security.cert.CertificateException
import javax.net.ssl.SSLHandshakeException
import kotlin.coroutines.CoroutineContext

class ApiCoroutineExceptionHandler: CoroutineExceptionHandler {

        override val key = CoroutineExceptionHandler

        override fun handleException(context: CoroutineContext, exception: Throwable) {
            when (exception) {
                is ApiErrorException -> {
//                    //处理业务错误
                }
                is JsonParseException -> {
                    //数据解析异常
                    ToastUtils.showShort("数据解析异常")
                }
                is CertificateException, is SSLHandshakeException -> {
                    //证书异常
                    ToastUtils.showShort("证书异常")
                }
                else -> {
                    ToastUtils.showShort("其他异常")
                    exception.printStackTrace()
                }
            }
        }

}